/*
 * ProfileSettingsView.java
 *
 * Created on 12 Kasım 2008 Çarşamba, 12:50
 */
package org.testis.gui.mvc.view.testdesign;

import org.testis.gui.mvc.view.*;
import org.testis.gui.mvc.controller.testdesign.ProfileSettingsController;
import org.testis.gui.mvc.controller.TestDesignControllerList;
import org.testis.gui.mvc.utilities.AtofTableModel;
import java.awt.Frame;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import javax.swing.JOptionPane;
import javax.swing.RowFilter;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author  nurhat
 */
public class ProfileSettingsView extends AtofBaseDialog implements
        AbstractViewPanel {

    private static final long serialVersionUID = 19874328L;
    private TableRowSorter<TableModel> sorter;

    /** Creates new form ProfileSettingsView */
    public ProfileSettingsView(java.awt.Frame parent,
            boolean modal) {
        super(parent,
                modal);
        initComponents();
    }

    public ProfileSettingsView(Frame parent,
            boolean modal,
            AtofTableModel tableModel) {
        super(parent,
                modal);
        initComponents();
        sorter = new TableRowSorter<TableModel>(tableModel);
        profilesTable.setRowSorter(sorter);
        profilesTable.setModel(tableModel);
    }

    private TableModelListener addListener() {
        return new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {
                try {
                    if (e.getType() ==
                            TableModelEvent.UPDATE) {
                        int column =
                                e.getColumn();
                        if (column > 0) {
                            int row =
                                     profilesTable.getSelectedRow();

                            String currentParameter =
                                    (String) profilesTable.getValueAt(
                                    row,
                                    0);
                            String currentValue =
                                    (String) profilesTable.getValueAt(
                                    row,
                                    1);
                            int id = Integer.parseInt(currentParameter);
                            TestDesignControllerList.newInstance().
                                    getProfileSettingsController().
                                    operationEditProfile(id,
                                    currentValue);
                             filterTable();
                        }
                    }
                } catch (Exception ex) {
                }
            }
        };
    }

    private void filterTable() {
        String text = txtFilter.getText();
        if (text.length() == 0) {
            sorter.setRowFilter(null);
        } else {
            sorter.setRowFilter(RowFilter.regexFilter("(?i)" +text));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        profilesTable = new javax.swing.JTable();
        btnAdd = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDetails = new javax.swing.JButton();
        btnShowKeys = new javax.swing.JButton();
        btnCreateCopy = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        txtFilter = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Profiles");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(241, 240, 235));

        profilesTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        profilesTable.setUpdateSelectionOnSort(false);
        jScrollPane1.setViewportView(profilesTable);

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnExit.setText("Exit");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        btnEdit.setText("Edit Name");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        btnDetails.setText("Show Details");
        btnDetails.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDetailsActionPerformed(evt);
            }
        });

        btnShowKeys.setText("Show Keys");
        btnShowKeys.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShowKeysActionPerformed(evt);
            }
        });

        btnCreateCopy.setText("Copy Selected");
        btnCreateCopy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateCopyActionPerformed(evt);
            }
        });

        jLabel4.setText("Filter:");

        txtFilter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtFilterActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 509, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(btnEdit)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnAdd)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnDelete)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnDetails)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnCreateCopy))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(btnShowKeys)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 262, Short.MAX_VALUE)
                                .addComponent(btnExit)
                                .addGap(15, 15, 15)))
                        .addGap(106, 106, 106))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtFilter, javax.swing.GroupLayout.PREFERRED_SIZE, 456, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(31, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtFilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEdit)
                    .addComponent(btnAdd)
                    .addComponent(btnDelete)
                    .addComponent(btnDetails)
                    .addComponent(btnCreateCopy))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnShowKeys)
                    .addComponent(btnExit))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
    try {
        int startingRow =
                profilesTable.getSelectedRow();
        if (startingRow >= 0) {
            String currentParameter =
                    (String) profilesTable.getValueAt(
                    startingRow,
                    0);
            String currentValue =
                    (String) profilesTable.getValueAt(startingRow,
                    1);
            int id = Integer.parseInt(currentParameter);
            String newValue = JOptionPane.showInputDialog("Enter new value:",
                    currentValue);
            if (newValue != null) {
                if (!newValue.equals(currentValue)) {
                    TestDesignControllerList.newInstance().
                            getProfileSettingsController().
                            operationEditProfile(id,
                            newValue);
                }
            }
            filterTable();
        }
    } catch (Exception ex) {
    }
}//GEN-LAST:event_btnEditActionPerformed

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
    String value = JOptionPane.showInputDialog("Enter  value:");
    if (value != null) {
        if (!value.trim().equals("")) {
            TestDesignControllerList.newInstance().getProfileSettingsController().
                    operationAddProfile(value);
             filterTable();
        }
    }
}//GEN-LAST:event_btnAddActionPerformed

private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
    int startingRow =
            profilesTable.getSelectedRow();
    if (startingRow >= 0) {
        String currentParameter =
                (String) profilesTable.getValueAt(
                startingRow,
                0);
        String currentValue =
                (String) profilesTable.getValueAt(startingRow,
                1);
        int result =
                javax.swing.JOptionPane.showConfirmDialog(null,
                "Confirmation",
                "Are you sure?",
                javax.swing.JOptionPane.YES_NO_CANCEL_OPTION,
                javax.swing.JOptionPane.QUESTION_MESSAGE);
        if (result == javax.swing.JOptionPane.YES_OPTION) {
            int id = Integer.parseInt(currentParameter);
            TestDesignControllerList.newInstance().getProfileSettingsController().
                    operationRemoveProfile(id);
             filterTable();
        }
    }
}//GEN-LAST:event_btnDeleteActionPerformed

private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
    this.setVisible(false);
    this.setEnabled(false);
    this.dispose();
}//GEN-LAST:event_btnExitActionPerformed

private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    WindowListener[] listeners = getWindowListeners();
    for (int i = 0; i < listeners.length; i++) {
        this.removeWindowListener(listeners[i]);
    }
    this.setVisible(false);
    this.setEnabled(false);
    TestDesignControllerList.newInstance().getProfileSettingsController().
            operationCloseWindow();
    this.dispose();
}//GEN-LAST:event_formWindowClosed

private void btnShowKeysActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowKeysActionPerformed
    TestDesignControllerList.newInstance().getProfileKeysController().
            startLoader();
}//GEN-LAST:event_btnShowKeysActionPerformed

private void btnDetailsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDetailsActionPerformed
    int startingRow =
            profilesTable.getSelectedRow();
    if (startingRow >= 0) {
        String currentParameter =
                (String) profilesTable.getValueAt(
                startingRow,
                0);
        String currentValue =
                (String) profilesTable.getValueAt(startingRow,
                1);
        int id = Integer.parseInt(currentParameter);
        TestDesignControllerList.newInstance().
                getProfileSettingDetailsController().startLoader(id);
    }
}//GEN-LAST:event_btnDetailsActionPerformed

private void btnCreateCopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateCopyActionPerformed
    int startingRow =
            profilesTable.getSelectedRow();
    if (startingRow >= 0) {
        String currentParameter =
                (String) profilesTable.getValueAt(
                startingRow,
                0);
        String currentValue =
                (String) profilesTable.getValueAt(startingRow,
                1);
        int id = Integer.parseInt(currentParameter);
        TestDesignControllerList.newInstance().
                getProfileSettingsController().operationCreateCopy(id);
         filterTable();
    }
}//GEN-LAST:event_btnCreateCopyActionPerformed

private void txtFilterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtFilterActionPerformed
    filterTable();
}//GEN-LAST:event_txtFilterActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCreateCopy;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnDetails;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnShowKeys;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable profilesTable;
    private javax.swing.JTextField txtFilter;
    // End of variables declaration//GEN-END:variables

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(
                ProfileSettingsController.SHOW_VIEW)) {
            TestDesignControllerList.newInstance().getProfileSettingsController().
                    operationLoadProfiles();
            this.centerParent();
        } else if (evt.getPropertyName().equals(
                ProfileSettingsController.SHOW_ERROR)) {
            String val = (String) evt.getNewValue();
            JOptionPane.showMessageDialog(
                    this,
                    val,
                    "Error Dialog",
                    JOptionPane.ERROR_MESSAGE);
        } else if (evt.getPropertyName().equals(
                ProfileSettingsController.PARAMETER_MODEL_CHANGED)) {
            profilesTable.setModel((AtofTableModel) evt.getNewValue());
            profilesTable.getColumnModel().getColumn(0).setMaxWidth(20);
            profilesTable.repaint();
            sorter = new TableRowSorter<TableModel>((AtofTableModel) evt.getNewValue());
            profilesTable.setRowSorter(sorter);
            profilesTable.getModel().addTableModelListener(addListener());
        }
    }
}
